package org.tio.sitexxx.im.server.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.sitexxx.im.common.Command;
import org.tio.sitexxx.im.common.CommandHandler;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.common.bs.P2PAlreadyReadReq;
import org.tio.sitexxx.im.server.utils.ImUtils;
import org.tio.sitexxx.service.model.main.IpInfo;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.service.base.IpInfoService;
import org.tio.sitexxx.service.service.stat.ChatRecordP2PService;
import org.tio.sitexxx.service.vo.Devicetype;
import org.tio.utils.json.Json;

/**
 * 我告诉服务器，张三发给我的私聊消息已读
 * @author tanyaowu 
 * 2016年9月13日 上午10:16:19
 */
@CommandHandler(Command.P2P_ALREADY_READ_REQ)
public class P2PAlreadyReadReqHandler implements ImServerHandler {
	@SuppressWarnings("unused")
	private static Logger log = LoggerFactory.getLogger(P2PAlreadyReadReqHandler.class);

	public static final P2PAlreadyReadReqHandler me = new P2PAlreadyReadReqHandler();

	public P2PAlreadyReadReqHandler() {
	}

	@Override
	public void handler(ImPacket packet, ChannelContext channelContext, boolean isWebsocket) throws Exception {
		User curr = ImUtils.getUser(channelContext);
		if (curr == null) {
//			ImUtils.pleaseLogin(channelContext, "登录才能聊天哦");
			return;
		}
		
		Devicetype devicetype = ImUtils.getDevicetype(channelContext);
		IpInfo ipInfo = IpInfoService.ME.save(channelContext.getClientNode().getIp());
		P2PAlreadyReadReq p2pAlreadyReadReq = Json.toBean(packet.getBodyStr(), P2PAlreadyReadReq.class);
		ChatRecordP2PService.me.updateToRead(p2pAlreadyReadReq.getFromuid(), curr.getId(), devicetype.getValue(), ipInfo.getId());
	}

}
